home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_05 / saks / lns5.cpp < prev    next >
C/C++ Source or Header  |  1994-03-23  |  3KB  |  178 lines

  1. //
  2. // lns5.cpp - line number sequence
  3. // implementation
  4. //
  5.  
  6. #include <stdio.h>
  7.  
  8. #include "lns.h"
  9.  
  10. //
  11. // lns_list
  12. //
  13. class lns_list : public lns::protocol
  14.     {
  15. public:
  16.     lns_list();
  17.     lns_list(unsigned n);
  18.     ~lns_list();
  19.     void print();
  20. protected:
  21.     struct node;
  22.     node *first;
  23.     };
  24.  
  25. struct lns_list::node
  26.     {
  27.     node(unsigned n);
  28.     unsigned number;
  29.     node *next;
  30.     };
  31.  
  32. inline lns_list::node::node(unsigned n)
  33.     : number(n), next(0)
  34.     {
  35.     }
  36.  
  37. //
  38. // lns_list member definitions
  39. //
  40. inline lns_list::lns_list()
  41.     {
  42.     }
  43.  
  44. inline lns_list::lns_list(unsigned n)
  45.     : first(new node(n))
  46.     {
  47.     }
  48.  
  49. lns_list::~lns_list()
  50.     {
  51.     node *p;
  52.     while ((p = first) != 0)
  53.         {
  54.         first = first->next;
  55.         delete p;
  56.         }
  57.     }
  58.  
  59. void lns_list::print()
  60.  
  61.     {
  62.     node *p;
  63.     for (p = first; p != 0; p = p->next)
  64.         printf(" %4d", p->number);
  65.     }
  66.  
  67. //
  68. // lns_small
  69. //
  70. class lns_small : public lns_list
  71.     {
  72. public:
  73.     lns_small(unsigned n);
  74.     protocol *add(unsigned n);
  75.     void print();
  76. private:
  77.     enum { THRESHOLD = 5 };
  78.     };
  79.  
  80. //
  81. // lns_fast
  82. //
  83. class lns_fast : public lns_list
  84.     {
  85. public:
  86.     lns_fast(node *fn, node *ln);
  87.     lns_fast(unsigned n);
  88.     protocol *add(unsigned n);
  89.     void print();
  90. private:
  91.     node *last;
  92.     };
  93.  
  94. //
  95. // lns_fast member definitions
  96. //
  97. inline lns_fast::lns_fast(node *fn, node *ln)
  98.     {
  99.     first = fn;
  100.     last = ln;
  101.     }
  102.  
  103. inline lns_fast::lns_fast(unsigned n)
  104.     : lns_list(n)
  105.     {
  106.     last = first;
  107.     }
  108.  
  109. lns::protocol *lns_fast::add(unsigned n)
  110.     {
  111.     if (last->number != n)
  112.         last = last->next = new node(n);
  113.     return this;
  114.     }
  115.  
  116. void lns_fast::print()
  117.     {
  118.     printf("  (f)");
  119.     lns_list::print();
  120.     }
  121.  
  122. //
  123. // lns_small member definitions
  124. //
  125. inline lns_small::lns_small(unsigned n)
  126.     : lns_list(n)
  127.     {
  128.     }
  129.  
  130. lns::protocol *lns_small::add(unsigned n)
  131.     {
  132.     int len = 1;
  133.     node *p = first;
  134.     for ( ; p->next != 0; p = p->next)
  135.         ++len;
  136.     if (p->number != n)
  137.         {
  138.         p = p->next = new node(n);
  139.         if (++len > THRESHOLD)
  140.             {
  141.             lns_fast *q =
  142.                 new lns_fast(first, p);
  143.             first = 0;
  144.             return q;
  145.             }
  146.         }
  147.     return this;
  148.     }
  149.  
  150. void lns_small::print()
  151.     {
  152.     printf("  (s)");
  153.     lns_list::print();
  154.     }
  155.  
  156. //
  157. // lns constructor
  158. //
  159. lns::lns(unsigned n)
  160.     {
  161.     pp = new lns_small(n);
  162.     }
  163.  
  164. void lns::add(unsigned n)
  165.     {
  166.     protocol *np = pp->add(n);
  167.     if (np != pp)
  168.         {
  169.         delete pp;
  170.         pp = np;
  171.         }
  172.     }
  173. lns::protocol::~protocol()
  174. { }
  175.  
  176. // End of File 
  177.  
  178.